package com.haixiaoke.saas.banner.service.impl;

import com.haixiaoke.saas.banner.domain.Banner;
import com.haixiaoke.saas.banner.mapper.BannerMapper;
import com.haixiaoke.saas.banner.service.IBannerService;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 广告信息Service业务层处理
 *
 * @author xiaolong
 * @date 2025-05-07
 */
@Service
public class BannerServiceImpl implements IBannerService {
    @Autowired
    private BannerMapper bannerMapper;

    /**
     * 查询广告信息
     *
     * @param bannerId 广告信息主键
     * @return 广告信息
     */
    @Override
    public Banner selectBannerByBannerId(Long bannerId) {
        return bannerMapper.selectBannerByBannerId(bannerId);
    }

    /**
     * 查询广告信息列表
     *
     * @param banner 广告信息
     * @return 广告信息
     */
    @Override
    public List<Banner> selectBannerList(Banner banner) {
        return bannerMapper.selectBannerList(banner);
    }

    /**
     * 新增广告信息
     *
     * @param banner 广告信息
     * @return 结果
     */
    @Override
    public int insertBanner(Banner banner) {
        verificationParameters(banner);
        banner.setCreateTime(DateUtils.getNowDate());
        return bannerMapper.insertBanner(banner);
    }

    private void verificationParameters(Banner banner) {
        if (banner.getTitle().length() > 20) {
            throw new ServiceException("标题长度不能超过20个字");
        }
        if (banner.getContent().length() > 200) {
            throw new ServiceException("内容长度不能超过200个字");
        }
    }

    /**
     * 修改广告信息
     *
     * @param banner 广告信息
     * @return 结果
     */
    @Override
    public int updateBanner(Banner banner) {
        verificationParameters(banner);
        banner.setUpdateTime(DateUtils.getNowDate());
        return bannerMapper.updateBanner(banner);
    }

    /**
     * 批量删除广告信息
     *
     * @param bannerIds 需要删除的广告信息主键
     * @return 结果
     */
    @Override
    public int deleteBannerByBannerIds(Long[] bannerIds) {
        return bannerMapper.deleteBannerByBannerIds(bannerIds);
    }

    /**
     * 删除广告信息信息
     *
     * @param bannerId 广告信息主键
     * @return 结果
     */
    @Override
    public int deleteBannerByBannerId(Long bannerId) {
        return bannerMapper.deleteBannerByBannerId(bannerId);
    }
}
